
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.UserBean;
import validation.ValidateForm;
import database.DBClass;
import java.util.List;

/**
 *
 * @author Jagadeesh
 */
public class ControllerServlet extends HttpServlet {

    //declare values to get form values from jsp page
    String Nama;
    String Alamat;
    String NoTelp;
    String JenisKelamin;
    String Email;
    String Username;
    String Password;
    String action;
    String EditUser;
    UserBean bean = new UserBean();
    ValidateForm validateform = new ValidateForm();
    DBClass dbobject = new DBClass();

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            //get the values from jsp page
            Nama = request.getParameter("Nama");
            Alamat = request.getParameter("Alamat");
            NoTelp = request.getParameter("NoTelp");
            JenisKelamin = request.getParameter("JenisKelamin");
            Email = request.getParameter("Email");
            Username = request.getParameter("Username");
            Password = request.getParameter("Password");
            action = request.getParameter("action");
            /*
            out.println (Nama);
            out.println (Alamat);
            out.println (NoTelp);
            out.println (JenisKelamin);
            out.println (Email);
            out.println (Username);
            out.println (Password);
          */
            
            if (action.equals("action")) {
                //set values to bean.For this call below method
                setValuesToBean();

                //check all form values are valid or not. send bean object 
                UserBean checkedbean = validateform.validateData(bean);
                if (!checkedbean.getIsValid()) {

                    //if data is invalid.set bean object in request and pass that request to
                    //insertupdate.jsp using forward
                    checkedbean.setAction("action");
                    request.setAttribute("error", checkedbean);

                    RequestDispatcher rd = request.getRequestDispatcher("signup2.jsp");
                    rd.forward(request, response);
                    //now display errors in that jsp page
                } else {
                    //using DBClass object call insertDetails method and pass bean object
                    dbobject.insertDetails(bean);
                    List list = dbobject.getAlldetails();
                    request.setAttribute("list", list);
                    //forward to insertupdate page using requestdispatcher
                    RequestDispatcher rd = request.getRequestDispatcher("view.jsp");
                    //display a message to client.store message in request object
                    //forwarding to jsp
                    rd.forward(request, response);


                }
            }
            if (action.equals("edit")) {
                //get userdetails of particular name
                UserBean ubean = dbobject.getDetails(Username);
                request.setAttribute("updateuser", ubean);
                RequestDispatcher rd = request.getRequestDispatcher("signup2.jsp");
                rd.forward(request, response);
            }
            
           
            if (action.equals("update")) {
                setValuesToBean();
                UserBean checkedbean = validateform.validateData(bean);
                System.out.println(checkedbean.getIsValid());
                if (!checkedbean.getIsValid()) {

                    //if data is invalid.set bean object in request and pass that request to
                    //insertupdate.jsp using forward
                    checkedbean.setAction("update");
                    request.setAttribute("error", checkedbean);
                    RequestDispatcher rd = request.getRequestDispatcher("signup2.jsp");
                    rd.forward(request, response);
                    //now display errors in that jsp page
                } else {
                    //using DBClass object call insertDetails method and pass bean object
                    dbobject.UpateDetails(bean, Username);
                    List list = dbobject.getAlldetails();
                    request.setAttribute("list", list);
                    //forward to insertupdate page using requestdispatcher
                    RequestDispatcher rd = request.getRequestDispatcher("view.jsp");
                    //display a message to client.store message in request object
                    //forwarding to jsp
                    rd.forward(request, response);


                }

            }
            if (action.equals("delete")) {
                //delete userdetails of particular name
                dbobject.deleteDetails(Username);
                List list = dbobject.getAlldetails();
                request.setAttribute("list", list);
                RequestDispatcher rd = request.getRequestDispatcher("view.jsp");
                rd.forward(request, response);
            }
                
        } catch (Exception e) {
            out.println(e);
        } finally {
            out.close();
        }
    }
    //this method is used to setvalues to bean

    public void setValuesToBean() {
        bean.setNama(Nama);
        bean.setAlamat(Alamat);
        bean.setNoTelp(NoTelp);
        bean.setJenisKelamin(JenisKelamin);
        bean.setEmail(Email);
        bean.setUsername(Username);
        bean.setPassword(Password);
        
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    public String getServletInfo() {
        return "Short description";
    }
}